# Maintainer: Felix Yan <felixonmars@archlinux.org>

_hkgname=brittany
pkgname=haskell-brittany
pkgver=0.14.0.2
pkgrel=91
pkgdesc="Haskell source code formatter"
url="https://github.com/lspitzner/brittany/"
license=("AGPL")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-aeson' 'haskell-butcher' 'haskell-cmdargs' 'haskell-czipwith'
         'haskell-data-tree-print' 'haskell-extra' 'haskell-ghc' 'haskell-ghc-exactprint'
         'haskell-monad-memo' 'haskell-multistate' 'haskell-random' 'haskell-safe'
         'haskell-strict' 'haskell-syb' 'haskell-uniplate' 'haskell-yaml')
makedepends=('ghc' 'uusi' 'haskell-hspec')
source=("https://hackage.haskell.org/packages/archive/$_hkgname/$pkgver/$_hkgname-$pkgver.tar.gz")
sha512sums=('966984304e1a156d96d7354030c7d3f62752ea4274d5aff0045e0be975fa7cf91ca02c779208fa6dd5cbf7e4c6f992b3f9efa279400574d5da8baca62b0ff8f3')

prepare() {
  cd $_hkgname-$pkgver
  uusi -r semigroups:Cabal -u aeson -u hspec $_hkgname.cabal
  gen-setup

  # Hack to use Cabal's compatibility layer of semigroups
  sed -i 's/import Data.Semigroup.Generic/import Distribution.Compat.Semigroup/' source/library/Language/Haskell/Brittany/Internal/Config/Types.hs
}

build() {
  cd $_hkgname-$pkgver

  runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
    --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
    --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm \
    --ghc-option=-optl-Wl\,-z\,relro\,-z\,now \
    --ghc-option='-pie'

  runhaskell Setup build $MAKEFLAGS
  runhaskell Setup register --gen-script
  runhaskell Setup unregister --gen-script
  sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
  sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
  cd $_hkgname-$pkgver
  mkdir output
  runhaskell Setup test --show-details=direct
}

package() {
  cd $_hkgname-$pkgver

  install -D -m744 register.sh "$pkgdir"/usr/share/haskell/register/$pkgname.sh
  install -D -m744 unregister.sh "$pkgdir"/usr/share/haskell/unregister/$pkgname.sh
  runhaskell Setup copy --destdir="$pkgdir"
  install -D -m644 LICENSE -t "$pkgdir"/usr/share/licenses/$pkgname/
  rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
